package cn.pegasus.teamwork.advertisement.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageHelper;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.advertisement.entity.BussAdvertInfo;
import cn.pegasus.teamwork.advertisement.mapper.BussAdvertInfoMapper;
import cn.pegasus.teamwork.advertisement.service.BussAdvertInfoService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.mapper.SysConfigDictionaryMapper;

/**
 * @author buyuer
 * @version
 */
@Service
public class BussAdvertInfoServiceImpl extends BaseServiceImpl<BussAdvertInfo, BussAdvertInfoMapper> implements BussAdvertInfoService {
    Logger logger = Logger.getLogger(this.getClass());

    // 注入当前dao对象
    @Autowired
    private BussAdvertInfoMapper bussAdvertInfoMapper;
    
    @Autowired
    private SysConfigDictionaryMapper sysConfigDictionaryMapper;
    
    
    public BussAdvertInfoServiceImpl() {
        setMapperClass(BussAdvertInfoMapper.class, BussAdvertInfo.class);
    }

	@Override
	public PageObject<BussAdvertInfo> PageList(Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
        int totalData = bussAdvertInfoMapper.getCount(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussAdvertInfo> list = bussAdvertInfoMapper.pageQueryByObject(pageHelper.getMap());
        if(null != list && list.size() > 0){
        	for(BussAdvertInfo p : list){
        		 SysConfigDictionary dictionary = new SysConfigDictionary();
        	        dictionary.setDictName(p.getAdvertPosition());
        	        dictionary.setCurrType(0);
        	        SysConfigDictionary c = sysConfigDictionaryMapper.selectSingle(dictionary);
        	        p.setDictName(c.getDictValue());
            }
        }
        PageObject<BussAdvertInfo> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}

	@Override
	public void addAdv(Long userId, BussAdvertInfo info) {
		info.setCreateTime(new Date());
		info.setCreateUserId(userId);
		info.setLastUpdateTime(new Date());
		info.setLastUpdateUserId(userId);
		info.setAdvertState(0);
		bussAdvertInfoMapper.insertByEntity(info);
	}

	@Override
	public void updateAdv(Long userId, BussAdvertInfo info) {
		info.setLastUpdateTime(new Date());
		info.setLastUpdateUserId(userId);
		bussAdvertInfoMapper.updateByEntity(info);
	}

	@Override
	public void deleteAdv(Long userId, Integer id) {
		BussAdvertInfo info = new BussAdvertInfo();
		info.setId(id);
		info.setLastUpdateUserId(userId);
		info.setLastUpdateTime(new Date());
		info.setAdvertState(1);
		bussAdvertInfoMapper.updateByEntity(info);
		
	}

	@Override
	public BussAdvertInfo findInfo(Integer id) {
		BussAdvertInfo info = new BussAdvertInfo();
		info.setId(id);
		return bussAdvertInfoMapper.selectSingle(info);
	}

}
